import { createStore } from 'vuex'
import { ElMessage } from "element-plus";
export default createStore({
  state: {
    snakeHistory: [],
    width: 30,
    height: 30,
    timer: null,
    key: '',
    initSpeed: 80,
    snake: null,
    food: {
      buff: '',
      x: 5,
      y: 5,
    },
    map: {
      color: 'blue',
      max: {
        x: 25,
        y: 18,
      },
      border: null
    }
  },
  getters: {
    score(state) {
      return state.snake.body.length
    },
    mapWidth(state) {
      return state.width * state.map.max.x
    },
    mapHeight(state) {
      return state.height * state.map.max.y
    },
    borderWidth(state) {
      return 1.2 * state.width
    },
    borderColor(state) {
      let re = ''
      switch (state.snake.buff) {
        case 'wall':
          re = 'red'
          break;
        default:
          re = 'black'
          break;
      }
      return re
    },
    head(state) {
      return state.snake.body[0]
    },
    snakeColor(state) {
      let re = ''
      switch (state.snake.buff) {
        case 'wall':
          re = 'red'
          break;
        case 'crossSelf':
          re = 'yellow'
          break;
        case 'slow':
          re = 'white'
          break;
        default:
          re = 'green'
          break;
      }
      return re
    },
    foodColor(state) {
      let re = ''
      switch (state.food.buff) {
        case 'wall':
          re = 'red'
          break;
        case 'crossSelf':
          re = 'yellow'
          break;
        case 'slow':
          re = 'white'
          break;
        default:
          re = 'grey'
          break;
      }
      return re
    },
  },
  mutations: {
    init(state) {
      state.key = 'right'
      state.snake = {
        color: 'green',
        buff: '',
        body: [
          { x: 2, y: 0 },
          { x: 1, y: 0 },
          { x: 0, y: 0 },
        ],
        direction: 'right'
      }
      state.snake.speed = state.initSpeed
      state.food = {
        color: 'black',
        buff: '',
        x: 5,
        y: 5,
      }
      state.map.border = {
        color: 'black',
        style: 'solid'
      }
    },
    start(state, loop) {
      clearInterval(state.timer)
      state.timer = setInterval(loop, this.state.snake.speed);
    },
    stop(state) {
      clearInterval(state.timer)
      ElMessage.warning({
        message: "游戏结束",
        type: "warning",
      });
      //需要保存得分
      let score = this.getters.score
      state.snakeHistory.push({ date: new Date(), score: score })
    },
    updateDirection(state) {
      switch (state.key) {
        case 'i':
          if (state.snake.direction != 'down')
            state.snake.direction = 'up'
          break;
        case 'k':
          if (state.snake.direction != 'up')
            state.snake.direction = 'down'
          break;
        case 'j':
          if (state.snake.direction != 'right')
            state.snake.direction = 'left'
          break;
        case 'l':
          if (state.snake.direction != 'left')
            state.snake.direction = 'right'
          break;
        default:
          break;
      }
    },
    updateFood(state) {
      //不应该刷新在蛇身上
      console.log('update food')
      function getX() {
        return Math.floor(Math.random() * state.map.max.x)
      }
      function getY() {
        return Math.floor(Math.random() * state.map.max.y)
      }
      let x = getX()
      let y = getY()
      do{
        x = getX()
        y = getY()
      }while (state.snake.body.find(p => p.x === x && p.y === y))
      state.food.x = x
      state.food.y = y
      //生成0-100的随机数 for buff
      let i = Math.random() * 100
      if (i <= 30) {
        //0-30则可以穿墙
        state.food.buff = "wall"
      } else if (30 < i && i <= 50) {
        //30-50可以穿自己
        state.food.buff = 'crossSelf'
      } else if (50 < i && i <= 99) {
        //50-70
        state.food.buff = 'slow'
      } else {
        //70以上就是普通的食物
        state.food.buff = ""
      }
    },
    moveForward(state) {
      let old_x = state.snake.body[0].x
      let old_y = state.snake.body[0].y
      switch (state.snake.direction) {
        case 'up':
          state.snake.body.unshift({ x: old_x, y: old_y - 1 });
          break;
        case 'down':
          state.snake.body.unshift({ x: old_x, y: old_y + 1 });
          break;
        case 'left':
          state.snake.body.unshift({ x: old_x - 1, y: old_y });
          break;
        case 'right':
          state.snake.body.unshift({ x: old_x + 1, y: old_y });
          break;
        default:
          break;
      }
    },
  },
  actions: {
  },
  modules: {
  }
})
